<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <!-- 登录的表单 -->
        <!-- :model ->告诉表单收集到的数据在哪个对象当中 :rules->为表单添加规则-->
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到苏科甄选</h2>
          <!-- prop:要校验的字段的名字 -->
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              v-model="loginForm.password"
              type="password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              class="login_btn"
              type="primary"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { ElNotification } from 'element-plus'
//引入用户相关的小仓库
import useUserStore from '@/store/modules/user'
//引入获取时间的工具方法
import { getTime } from '@/utils/time'
let userStore = useUserStore()
// 获取el-form组件
let loginForms = ref()
//获取路由器
let $router = useRouter()
//收集表单数据
let loginForm = reactive({ username: 'admin', password: '111111' })
//定义按钮加载变量控制按钮加载
let loading = ref(false)
// 登录按钮回调
const login = async () => {
  //全部校验通过再发请求
  await loginForms.value.validate()
  //加载效果
  loading.value = true
  //点击登录按钮以后做什么？
  // 通知仓库发登陆请求
  // 请求成功->首页展示数据的地方
  // 登录失败->弹出登录失败信息

  try {
    //保证登陆成功
    await userStore.userLogin(loginForm)
    //编程式导航跳转到展示数据首页
    $router.push('/')
    //登录成功的提示信息
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `HI,${getTime()}好`,
    })
    loading.value = false
  } catch (error) {
    //登录失败的提示信息
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
    loading.value = false
  }
}
//自定义校验规则函数
const validatorUserName = (rule: any, value: any, callback: any) => {
  //rule:校验规则对象
  //value:表单元素文本内容
  //callback:
  // 如果符合条件通过callback放行
  // 如果不符合条件，通过callback方法注入错误信息
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('账号长度至少五位'))
  }
}
const validatorPassword = (rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码长度至少六位'))
  }
}
// 定义表单验证的规则
const rules = {
  //规则对象属性:
  //required,代表必填，不能为空
  //min,max:为最小和最大输入长度
  //message:错误的提示信息
  //trigger:触发校验的时机 change ->文本发生变化的时候 | blur ->表单失去焦点的时候
  username: [
    // { required: true, min: 5, max: 10, message: '账号长度应该在5-10个字符之间', trigger: 'change' }
    { trigger: 'change', validator: validatorUserName },
  ],
  password: [
    // { required: true, min: 6, max: 15, message: '密码长度至少6位', trigger: 'change' }
    { trigger: 'change', validator: validatorPassword },
  ],
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0px;
    }

    .el-form-item {
      margin: 20px 0px;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>
